﻿using CRM.Models;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using CRM.Enums;

namespace CRM.Services
{
    public class UserGroupService : BaseService
    {
        private readonly MyDbContext _dbContext;

        public UserGroupService(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 初始化用户组
        /// </summary>
        /// <param name="CompanyId"></param>
        /// <param name="OemId"></param>
        /// <returns></returns>
        public async Task<int> InitUserGroup(int CompanyId,int OemId,string Name = "初始_用户组") {
            UserGroup UserGroupModel = new() {
                Name= Name,
                State = States.ENABLE,
                Sort = -Helper.GetNowTimeStamp(),
                CompanyId = CompanyId,
                OemId = OemId
            };
            await _dbContext.UserGroup.AddAsync(UserGroupModel);
            await _dbContext.SaveChangesAsync();

            return UserGroupModel.Id;
        }

        /// <summary>
        /// 获取公司下有效用户组
        /// </summary>
        public record ChooseData(int Id, string Name);
        public List<ChooseData> GetAllGroup(int CompanyId, int OemId) {
            var Groups = _dbContext.UserGroup.
                Where(i => i.State == States.ENABLE).
                Where(i => i.CompanyId == CompanyId).
                Where(i => i.OemId == OemId).
                Select(n => new ChooseData
           (
               n.Id,
               n.Name
           )).ToList();
            return Groups;
        }
    }
}
